<?php

class Official_order_model extends CI_Model {

    function OfficialOrderModel() {
        parent::Model();
    }

    function get_official_order($order_type = 1) {

        $aColumns = array('ID', 'Order_Number', 'date', 'Ministry_ID');

        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Official_Order.ID", "ASC");
        }
        $order = $this->db->ar_orderby;

//        print_r(check_get("order_type"));
        $where = "order_type = '" . check_get("order_type") . "'";
        if ($_GET['sofficialno'] != '' && trim($_GET['sofficialno']) != 'លេខ​') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " Order_Number LIKE '%" . check_get('sofficialno') . "%' ";
        }
        if ($_GET['sfromdate'] != '' && strpos($_GET['sfromdate'], '/')) {
            $where .= $where != "" ? " AND " : " ";
            $where .= " date >= '" . $this->db->convertDate(check_get('sfromdate')) . "'";
            ;
        }
        if ($_GET['stodate'] != '' && strpos($_GET['stodate'], '/')) {
            $where .= $where != "" ? " AND " : " ";
            $where .= " date <= '" . $this->db->convertDate(check_get('stodate')) . "'";
        }
        if ($_GET['sprovince'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " province_ID = " . check_get('sprovince');
        }
        if ($_GET['scommune'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " commune_ID = " . check_get('scommune');
        }
        if ($_GET['sdistrict'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " district_ID = " . check_get('sdistrict');
        }
        if ($_GET['svillage'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " village_ID = " . check_get('svillage');
        }
        if ($_GET['sstatus'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " order_status = " . check_get('sstatus');
        }
        if ($_GET['sministry'] != '') {
            $where .= $where != "" ? " AND " : " ";
            $where .= " Ministry_ID = " . check_get('sministry');
        }
        if ($where != "") {
            $this->db->where($where);
        }

        $this->db->select('Official_Order.ID');
        $this->db->join('Ministry', 'Ministry.ID= Official_Order.Ministry_ID', 'inner');
        $totalQuery = $this->db->get('Official_Order');
        if ($where != "") {
            $this->db->where($where);
        }

        $this->db->select('Official_Order.ID, Official_Order.Order_Number, Official_Order.date, Ministry.name, Official_Order.order_file');
        $this->db->join('Ministry', 'Ministry.ID= Official_Order.Ministry_ID', 'left');
        $query = $this->db->get('Official_Order', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $official_orders = $query->result_array();
        $aaData = array();
        $i = check_get('iDisplayStart');
        foreach ($official_orders as $official_order) {
            if (trim($official_order['order_file']) != "") {
                $url = "<a href='" . base_url() . "public/files/{$official_order['order_file']}'  title='ដោនឡូដ' target='_blank'><img align='absmiddle' src='" . base_url() . "public/images/forms/download.png' /></a>";
            } else {
                $url = "";
            }
            $data = array(++$i, $official_order['Order_Number'], date("d/m/Y", strtotime($official_order['date'])), $official_order['name'], $url, "<a class='edit-btn edit-official' title='កែប្រែ' rel='{$official_order['ID']}'>&nbsp;</a>", "<a class='delete-btn delete-official' rel='{$official_order['ID']}' title='លុប'>&nbsp;</a>");
            array_push($aaData, $data);
        }


        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

    function checkExistingOfficialNumber($file_name) {
        $return = array();
        $query = $this->db->get_where('Official_Order', array('Order_Number' => check_post('officialno')));
        if (!$query->num_rows) {
            $date = $this->db->convertDate(check_post('officialdate'));
            $this->db->insert('Official_Order', array(
                'Order_Number' => check_post('officialno'),
                'Ministry_ID' => check_post('ministry'),
                '`date`' => $date,
                'order_status' => check_post('status'),
                'order_type' => check_post('order_type'),
                'order_file' => $file_name,
                'province_ID' => check_post('province'),
                'district_ID' => check_post('district'),
                'commune_ID' => check_post('commune'),
                'village_ID' => check_post('village'),
            ));
            // combine_session('success', 'Added new officer successfully.');
            $return['result'] = true;
            $return['message'] = 'បន្ថែមទិន្នន័យថ្មីបានជោគជ័យ.';
        } else {
            $return['result'] = false;
            $return['message'] = 'លេខក្រឹតធ្លាប់មានរួចហើយនៅក្នុងប្រព័ន្ធ.';
        }
        return $return;
    }

    function deleteOfficialOrder() {
        $this->db->delete('Official_Order', array('ID' => check_post("ID")));
    }

    function read() {
        $query = $this->db->get_where('Official_Order', array('ID' => check_post('id')));
        return $query->result_array();
    }

    function update($file_name = '') {
        $return = array();
        $query = $this->db->get_where('Official_Order', array('Order_Number' => check_post('officialno'), "ID <> " => check_post('officialid')));
        if (!$query->num_rows) {
            $date = $this->db->convertDate(check_post('officialdate'));
            $this->db->where('ID', check_post('officialid'));
            if ($file_name != '') {
                $this->db->update('Official_Order', array(
                    'Order_Number' => check_post('officialno'),
                    'Ministry_ID' => check_post('ministry'),
                    '`date`' => $date,
                    'order_status' => check_post('status'),
                    'order_file' => $file_name,
                    'province_ID' => check_post('province'),
                    'district_ID' => check_post('district'),
                    'commune_ID' => check_post('commune'),
                    'village_ID' => check_post('village'),
                ));
            } else {
                $this->db->update('Official_Order', array(
                    'Order_Number' => check_post('officialno'),
                    'Ministry_ID' => check_post('ministry'),
                    '`date`' => $date,
                    'order_status' => check_post('status'),
                    'province_ID' => check_post('province'),
                    'district_ID' => check_post('district'),
                    'commune_ID' => check_post('commune'),
                    'village_ID' => check_post('village'),
                ));
            }
            // combine_session('success', 'Added new officer successfully.');
            $return['result'] = true;
            $return['message'] = 'កែប្រែទិន្នន័យបានជោគជ័យ.';
        } else {
            $return['result'] = false;
            $return['message'] = 'ទិន្នន័យធ្លាប់មានរួចហើយ សូមព្យាយាមម្តងទៀត.';
        }
        return $return;
    }

    function getOfficial($condition = "") {
        $q = "SELECT Official_Order.ID , Official_Order.Order_Number  FROM Official_Order ";
        if ($condition != "") {
            $q .= " WHERE Order_Number LIKE '%$condition%' ";
        }
        $query = $this->db->query($q." LIMIT 300 ");
        return $query->result_array();
    }

    public function getNewOfficial() {
        $aColumns = array('Official_Order.ID', 'Official_Order.Order_Number', 'Official_Order.date', 'Official_Order.order_status');
        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Official_Order.ID", "ASC");
        }
        $order = $this->db->ar_orderby;
        $where = " Official_Order.order_type = 1";
        $this->db->where($where);

        $this->db->select('Official_Order.ID');
        $this->db->join('Official_status', 'Official_status.ID= Official_Order.order_status', 'inner');
        $totalQuery = $this->db->get('Official_Order');
        if ($where != "") {
            $this->db->where($where);
        }
        $this->db->select('Official_Order.ID, Official_Order.Order_Number, Official_Order.date, Official_status.name');
        $this->db->join('Official_status', 'Official_status.ID= Official_Order.order_status', 'inner');
        $query = $this->db->get('Official_Order', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $official_orders = $query->result_array();
        $aaData = array();
        $i = check_get('iDisplayStart');
        foreach ($official_orders as $official_order) {
            $data = array(++$i, $official_order['Order_Number'], date("d/m/Y", strtotime($official_order['date'])), $official_order['name']);
            array_push($aaData, $data);
        }

        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

    public function getOfficialFollow() {
        $aColumns = array('Official_Order.ID', 'Official_Order.Order_Number', 'Official_Order.date', 'Official_Order.order_status');
        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Official_Order.ID", "ASC");
        }
        $order = $this->db->ar_orderby;
        $where = " Official_Order.order_type = 1 AND order_status = 1";
        $this->db->where($where);

        $this->db->select('Official_Order.ID');
        $this->db->join('Official_status', 'Official_status.ID= Official_Order.order_status', 'inner');
        $totalQuery = $this->db->get('Official_Order');
        if ($where != "") {
            $this->db->where($where);
        }
        $this->db->select('Official_Order.ID, Official_Order.Order_Number, Official_Order.date, Official_status.name');
        $this->db->join('Official_status', 'Official_status.ID= Official_Order.order_status', 'inner');
        $query = $this->db->get('Official_Order', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $official_orders = $query->result_array();
        $aaData = array();
        $i = check_get('iDisplayStart');
        foreach ($official_orders as $official_order) {
            $data = array(++$i, $official_order['Order_Number'], date("d/m/Y", strtotime($official_order['date'])), $official_order['name']);
            array_push($aaData, $data);
        }

        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

}

?>